package ginx

import (
	"gitee.com/xiao_hange/go-admin-pkg/pkg/logger"
	"github.com/gin-gonic/gin"
	"net/http"
)

var L logger.AppLogger

func WrapBody[T any](fn func(ctx *gin.Context, req T) Result) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req T
		if err := ctx.Bind(&req); err != nil {
			ctx.JSON(http.StatusOK, Result{Code: 4, Msg: "参数解析错误"})
			return
		}
		res := fn(ctx, req)
		ctx.JSON(http.StatusOK, res)
	}
}

type Result struct {
	// 这个叫做业务错误码
	Code  int    `json:"code"`
	Count int    `json:"count"`
	Msg   string `json:"msg"`
	Data  any    `json:"data"`
}
